package com.bishe.controller;

import com.bishe.entity.Project;
import com.bishe.service.ProjectService;
import com.bishe.utils.LayDataResult;
import com.bishe.utils.LayDataToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("project")
public class ProjectController {
    //注入对象
    @Autowired
    private ProjectService projectService;
    @RequestMapping("index1")
    public String index1(){
        return "/index.html";
    }

    @RequestMapping("index")
    public String index(){
        return "/extractSystem/addProject.html";
    }

    //新建一个project项目
    @RequestMapping("insert1")
    public String insert(Project project){
        projectService.insert1(project);
        return "redirect:projectList";
    }
    //跳转到projectList页面
    @RequestMapping("projectList")
    public String projectList(){
        return "/extractSystem/projectList.html";
    }

    //分页查询数据库中的project信息并转换为json数据
    @RequestMapping("projectJson")
    @ResponseBody
    public String  selectByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5")  int limit,
            HttpServletResponse response
    ){
        String json = "";

        /*
         * 需要的数据  当前第page页，共pageCount页
         * */
        //从数据库获取数据
        List<Project> list =projectService.selectByPage(page,limit);
        //获取总页数
        Long pageCount = projectService.getPageCount(limit);
        LayDataToJson layDataToJson = new LayDataToJson();
        LayDataResult layDataResult = new LayDataResult(0,"查询成功",pageCount,list);
        try {
             json = layDataToJson.toJson(layDataResult,response);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("json数据转换异常");
        }
        return json;
    }

}
